package com.liangge.essaysite.spider;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import com.liangge.essaysite.bean.Article;
import com.liangge.essaysite.bean.Category;
import com.liangge.essaysite.service.ArticleService;
import com.liangge.essaysite.util.AgentKeys;
import com.liangge.essaysite.util.CfgUtils;
import com.liangge.essaysite.util.HttpClientUtils;

public class GoodmoodSpider implements Runnable {

	private ArticleService articleService;
	private String articleUrl;

	public GoodmoodSpider(ArticleService articleService, String articleUrl) {
		this.articleService = articleService;
		this.articleUrl = articleUrl;
	}

	@Override
	public void run() {
		Article article = new Article();
		//文章ID
		Integer articleId = Integer.valueOf(StringUtils.substringBetween(articleUrl, "_", "."));
		article.setArticleId(articleId);
		//获取HTML代码解析
		String articleHtml = HttpClientUtils.httpGet(articleUrl);
		Document document = Jsoup.parse(articleHtml);
		//获取文章标题
		Elements titleElements = document.select("div.tit>h1");
		String title = titleElements.text();
		article.setArticleName(title);
		//获取分类
		Elements categoryElements = document.select("div.tit_zz>a");
		String categoryName = categoryElements.get(0).ownText();
		article.setCategory(new Category(getCategoryId(categoryName)));
		//获取文章内容
		Elements contentElements = document.select("div#zoom");
		Whitelist whitelist = new Whitelist();
		whitelist.addTags("br");
		String contentHtml = Jsoup.clean(contentElements.outerHtml(), whitelist);
		String content = StringUtils.replace(contentHtml, "<br />", "\r\n");
		article.setContent(content);
		
		article.setArticleSource(AgentKeys.ARTICLE_SOURCE_GOODMOOD);
		Integer defaultStatus = CfgUtils.getCommonCfg().getInteger(AgentKeys.P_SPIDER_ARTICLE_STATUS, AgentKeys.ARTICLE_STATUS_OFFLINE);
		article.setStatus(defaultStatus);
		
		//插入文章
		articleService.insertArticle(article);
	}
	
	/**
	 * 文章类别转换
	 * 1--感情
	 * 2--生活
	 * 3--青春
	 * 4--杂文
	 * 5--其它
	 * @author liangge
	 */
	private Integer getCategoryId(String categoryName){
		if(StringUtils.equals(categoryName, "感悟生活")){
			return 2;
		}else if(StringUtils.equals(categoryName, "爱情滋味")){
			return 1;
		}else if(StringUtils.equals(categoryName, "挚爱亲情")){
			return 1;
		}else if(StringUtils.equals(categoryName, "友情天地")){
			return 3;
		}else if(StringUtils.equals(categoryName, "青春校园")){
			return 3;
		}else if(StringUtils.equals(categoryName, "婚姻物语")){
			return 1;
		}else if(StringUtils.equals(categoryName, "河山雅韵")){
			return 4;
		}else if(StringUtils.equals(categoryName, "随笔小札")){
			return 4;
		}
		return 5;
	}

}
